package com.cz.timi.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/**
 * @Author: ChengZhe
 * @Date: 2018/6/13 17:25
 */
public class TuLingUtil {
    public static String getResult(String msg){
        StringBuffer sb = new StringBuffer();
        try {
            String question = URLEncoder.encode(msg, "utf-8");
            String requestUrl = "http://www.tuling123.com/openapi/api?"+"key=c72ed16d366f4708bc82de2ee17ddabd"+"&info="+question;
            URL urlObj = new URL(requestUrl);
            URLConnection connection = urlObj.openConnection();
            connection.connect();

            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
            String line = "";
            while ((line = reader.readLine()) != null){
                sb.append(line);
            }
            reader.close();

        } catch (Exception e){
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String res = getResult("你好啊");
        System.out.println(res);
    }
}
